/*
 * @Author: wuqingchun
 * @Date: 2024-03-20 17:16:39
 * @Description: socket 状态类定义
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-03-24 17:46:56
 */
#ifndef __UTCP_STATE_H__
#define __UTCP_STATE_H__

#include "utcp_socket.h"

// 状态模式，定义状态抽象类（类似于Java中的接口Interface）
class IUTCPState
{
public:
    // virtual IUTCPState* Open() =0;
    // virtual IUTCPState* Bind() = 0;
    // virtual IUTCPState* Listen() =0;
    // virtual IUTCPState* Accept() = 0;
    // virtual IUTCPState* Send() = 0;
    // virtual IUTCPState* Recv() = 0;
    // virtual IUTCPState* Close() = 0;

    virtual IUTCPState* Open()
    {
        return nullptr;
    }

    virtual IUTCPState* Bind() 
    {
        return nullptr;
    }

    virtual IUTCPState* Listen() 
    {
        return nullptr;
    }
    
    virtual IUTCPState* Accept() 
    {
        return nullptr;
    }

    virtual IUTCPState* Send() 
    {
        return nullptr;
    }

    virtual IUTCPState* Recv() 
    {
        return nullptr;
    }

    virtual IUTCPState* Close() 
    {
        return nullptr;
    }
};

class CUTCPSocketBase
{
public:
    virtual IUTCPState& GetClosedState() = 0;
    virtual IUTCPState& GetListenState() = 0;
    virtual IUTCPState& GetSyncSentState() = 0;
    virtual IUTCPState& GetSyncRecvState() = 0;
    virtual IUTCPState& GetEstablishedState() = 0;
    virtual IUTCPState& GetFinWait1State() = 0;
    virtual IUTCPState& GetTimeWaitState() = 0;
    virtual IUTCPState& GetCloseWaitState() = 0;
    virtual IUTCPState& GetNullState() = 0;
};

//class CUTCPSocketBase; // forward declaration(前向声明)

class CUTCPClosedState : public IUTCPState
{
private:
    CUTCPSocketBase& m_refContext;
public:
    CUTCPClosedState(CUTCPSocketBase& refContext): m_refContext(refContext)
    {
        
    }

    virtual IUTCPState* Open()
    {
        return  this;
    }

    virtual IUTCPState* Bind()
    {
        return  (IUTCPState*)&m_refContext.GetListenState();
    }

    virtual IUTCPState* Listen()
    {
        return  this;
    }

    virtual IUTCPState* Accept()
    {
        return  this;
    }

    virtual IUTCPState* Send()
    {
        return  this;
    }

    virtual IUTCPState* Recv()
    {
        return  this;
    }

    virtual IUTCPState* Close()
    {
        return  this;
    }
};

class CUTCPOpenState : public IUTCPState
{
private:
    CUTCPSocketBase& m_refContext;
public:
    CUTCPOpenState(CUTCPSocketBase& refContext): m_refContext(refContext)
    {
        
    }

    virtual IUTCPState* Bind() override
    {
        return this;
    }
};

class CUTCPListenState : public IUTCPState
{
private:
    CUTCPSocketBase& m_refContext;
public:
    CUTCPListenState(CUTCPSocketBase& refContext): m_refContext(refContext)
    {
        
    }

    virtual IUTCPState* Open()
    {
        return  this;
    }

    virtual IUTCPState* Bind()
    {
        return  this;
    }

    virtual IUTCPState* Listen()
    {
        return  this;
    }

    virtual IUTCPState* Accept()
    {
        return  this;
    }

    virtual IUTCPState* Send()
    {
        return  this;
    }

    virtual IUTCPState* Recv()
    {
        return  this;
    }

    virtual IUTCPState* Close()
    {
        return  this;
    }
};

class CUTCPSyncRecvState : public IUTCPState
{
private:
    CUTCPSocketBase& m_refContext;
public:
    CUTCPSyncRecvState(CUTCPSocketBase& refContext): m_refContext(refContext)
    {
        
    }

    virtual IUTCPState* Open()
    {
        return  this;
    }

    virtual IUTCPState* Bind()
    {
        return  this;
    }

    virtual IUTCPState* Listen()
    {
        return  this;
    }

    virtual IUTCPState* Accept()
    {
        return  this;
    }

    virtual IUTCPState* Send()
    {
        return  this;
    }

    virtual IUTCPState* Recv()
    {
        return  this;
    }

    virtual IUTCPState* Close()
    {
        return  this;
    }
};

class CUTCPSyncSentState : public IUTCPState
{
private:
    CUTCPSocketBase& m_refContext;
public:
    CUTCPSyncSentState(CUTCPSocketBase& refContext): m_refContext(refContext)
    {
        
    }

    virtual IUTCPState* Open()
    {
        return  this;
    }

    virtual IUTCPState* Bind()
    {
        return  this;
    }

    virtual IUTCPState* Listen()
    {
        return  this;
    }

    virtual IUTCPState* Accept()
    {
        return  this;
    }

    virtual IUTCPState* Send()
    {
        return  this;
    }

    virtual IUTCPState* Recv()
    {
        return  this;
    }

    virtual IUTCPState* Close()
    {
        return  this;
    }
};


class CUTCPEstablishedState : public IUTCPState
{
private:
    CUTCPSocketBase& m_refContext;
public:
    CUTCPEstablishedState(CUTCPSocketBase& refContext): m_refContext(refContext)
    {
        
    }

    virtual IUTCPState* Open()
    {
        return  this;
    }

    virtual IUTCPState* Bind()
    {
        return  this;
    }

    virtual IUTCPState* Listen()
    {
        return  this;
    }

    virtual IUTCPState* Accept()
    {
        return  this;
    }

    virtual IUTCPState* Send()
    {
        return  this;
    }

    virtual IUTCPState* Recv()
    {
        return  this;
    }

    virtual IUTCPState* Close()
    {
        return  this;
    }
};


class CUTCPFinWait1State : public IUTCPState
{
private:
    CUTCPSocketBase& m_refContext;
public:
    CUTCPFinWait1State(CUTCPSocketBase& refContext): m_refContext(refContext)
    {
        
    }

    virtual IUTCPState* Open()
    {
        return  this;
    }

    virtual IUTCPState* Bind()
    {
        return  this;
    }

    virtual IUTCPState* Listen()
    {
        return  this;
    }

    virtual IUTCPState* Accept()
    {
        return  this;
    }

    virtual IUTCPState* Send()
    {
        return  this;
    }

    virtual IUTCPState* Recv()
    {
        return  this;
    }

    virtual IUTCPState* Close()
    {
        return  this;
    }
};


class CUTCPFinWait2State : public IUTCPState
{
private:
    CUTCPSocketBase& m_refContext;
public:
    CUTCPFinWait2State(CUTCPSocketBase& refContext): m_refContext(refContext)
    {
        
    }

    virtual IUTCPState* Open()
    {
        return  this;
    }

    virtual IUTCPState* Bind()
    {
        return  this;
    }

    virtual IUTCPState* Listen()
    {
        return  this;
    }

    virtual IUTCPState* Accept()
    {
        return  this;
    }

    virtual IUTCPState* Send()
    {
        return  this;
    }

    virtual IUTCPState* Recv()
    {
        return  this;
    }

    virtual IUTCPState* Close()
    {
        return  this;
    }
};

class CUTCPCloseWaitState : public IUTCPState
{
private:
    CUTCPSocketBase& m_refContext;
public:
    CUTCPCloseWaitState(CUTCPSocketBase& refContext): m_refContext(refContext)
    {
        
    }

    virtual IUTCPState* Open()
    {
        return  this;
    }

    virtual IUTCPState* Bind()
    {
        return  this;
    }

    virtual IUTCPState* Listen()
    {
        return  this;
    }

    virtual IUTCPState* Accept()
    {
        return  this;
    }

    virtual IUTCPState* Send()
    {
        return  this;
    }

    virtual IUTCPState* Recv()
    {
        return  this;
    }

    virtual IUTCPState* Close()
    {
        return  this;
    }
};

class CUTCPTimeWaitState : public IUTCPState
{
private:
    CUTCPSocketBase& m_refContext;
public:
    CUTCPTimeWaitState(CUTCPSocketBase& refContext): m_refContext(refContext)
    {
        
    }

    virtual IUTCPState* Open()
    {
        return  this;
    }

    virtual IUTCPState* Bind()
    {
        return  this;
    }

    virtual IUTCPState* Listen()
    {
        return  this;
    }

    virtual IUTCPState* Accept()
    {
        return  this;
    }

    virtual IUTCPState* Send()
    {
        return  this;
    }

    virtual IUTCPState* Recv()
    {
        return  this;
    }

    virtual IUTCPState* Close()
    {
        return  this;
    }
};

class CUTCPNullState : public IUTCPState
{
public:
    CUTCPNullState()
    {
        
    }

    virtual IUTCPState* Open()
    {
        return  this;
    }

    virtual IUTCPState* Bind()
    {
        return  this;
    }

    virtual IUTCPState* Listen()
    {
        return  this;
    }

    virtual IUTCPState* Accept()
    {
        return  this;
    }

    virtual IUTCPState* Send()
    {
        return  this;
    }

    virtual IUTCPState* Recv()
    {
        return  this;
    }

    virtual IUTCPState* Close()
    {
        return  this;
    }
};


#endif //__UTCP_STATE_H__